package com.glodon.paas.activity.biz;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.glodon.paas.activity.dao.AppInfoDao;
import com.glodon.paas.activity.dao.domain.AppInfoEntity;
import com.glodon.paas.exception.PaasServerExceptionFactory;

@Component("appInfoBiz")
public class AppInfoBiz {

    @Autowired
    AppInfoDao appInfoDao;

    public Integer insert(AppInfoEntity record) {
        if (record == null) {
            return null;
        }
        if (record.getAppId() < 0 || record.getAppId() > 999999) {
            throw PaasServerExceptionFactory.validateException("error appId:" + record.getAppId(), null);
        }

        Date currentDate = new Date();
        if (record.getCreateTime() == null) {
            record.setCreateTime(currentDate);
        }
        if (record.getUpdateTime() == null) {
            record.setUpdateTime(currentDate);
        }

        return appInfoDao.insertSelective(record);
    };

    public int deleteById(Integer id) {
        return appInfoDao.deleteByPrimaryKey(id);
    };

    public int updateById(AppInfoEntity record) {
        return appInfoDao.updateByPrimaryKeySelective(record);
    };

    public AppInfoEntity selectById(Integer id) {
        return appInfoDao.selectByPrimaryKey(id);
    };

    public List<AppInfoEntity> selectAll() {
        return appInfoDao.selectAll();
    }

    public Map<Integer, AppInfoEntity> selectAllMap() {
        List<AppInfoEntity> list = appInfoDao.selectAll();
        Map<Integer, AppInfoEntity> map = new HashMap<Integer, AppInfoEntity>();
        for (AppInfoEntity entity : list) {
            map.put(entity.getAppId(), entity);
        }
        return map;
    };
}
